package com.java.androidmarket.controller;

import com.java.androidmarket.aspect.CheckUser;
import com.java.androidmarket.dto.GoodsDTO;
import com.java.androidmarket.dto.cart.DeleteGoodsDTO;
import com.java.androidmarket.entity.Cart;
import com.java.androidmarket.entity.Goods;
import com.java.androidmarket.entity.Result;
import com.java.androidmarket.service.CartService;
import com.java.androidmarket.service.StockService;
import com.java.androidmarket.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * description:
 * author:张腾
 * date:2021-04-30
 */

@RestController
@Api(tags = "购物车模块")
@RequestMapping("cart")
@CrossOrigin
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private StockService stockService;
    @Autowired
    private RedisUtils redisUtils;


    @GetMapping("getCart")
    @ApiOperation(value = "获取购物车" ,notes = "根据用户ID获取该用户的购物车")
    @ApiImplicitParam(dataType = "int", name = "userId", value = "用户ID")
//    @CheckUser
    public Result getCart(int userId){

        List<Goods> cartGoodsList = new ArrayList<>();
        int total = 0;
        String key = "Cart:"+userId;

        if (redisUtils.exists(key)){
            System.out.println("redis查找购物车");
            cartGoodsList = (List<Goods>) redisUtils.hmGet(key,"CartGoods");
            total = (int) redisUtils.hmGet(key,"Total");
        }else {
            System.out.println("数据库查找购物车");
            cartGoodsList = cartService.getCart(userId);
            redisUtils.hmSet(key,"CartGoods",cartGoodsList);
            redisUtils.hmSet(key,"Total",cartGoodsList.size());
        }


        //TODO
//        List<GoodsDTO> goodsDTOS = new ArrayList<>();
//        for (Goods cartGoods : cartGoodsList) {
//
//        }
        return Result.success(cartGoodsList,total);

    }

    @PostMapping(value = "addCart",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "添加购物车" ,notes = "为指定用户购物车中添加商品")
    @ApiImplicitParam(dataType = "Cart", name = "cart", value = "cart购物车实体类")
    public Result addCart(@RequestBody Cart cart){

        int stock = stockService.getStock(cart.getUserId());
        if (cart.getGoodsNum()<=stock) {

            if (redisUtils.exists("Cart:"+cart.getUserId())) {
                redisUtils.del("Cart:"+cart.getUserId());
            }

            cartService.addCart(cart.getUserId(),cart.getGoodsId());

            return Result.success();
        }else{
            return Result.success("库存不足！");
        }

    }

    @PostMapping("update")
    @ApiOperation(value = "修改数量", notes = "修改用户添加到购物车中的商品数量")
    @ApiImplicitParam(dataType = "Cart", name = "cart", value = "cart购物车实体类")
    public Result updateGoodsNum(@RequestBody Cart cart){
        int userId = cart.getUserId();
        int goodsId = cart.getGoodsId();
        int goodsNum = cart.getGoodsNum();

        int stock = stockService.getStock(cart.getGoodsId());
        if (goodsNum<=stock){
            if (redisUtils.exists("Cart:"+userId)) {
                redisUtils.del("Cart:"+userId);
            }
            int updateGoodsNum = cartService.updateGoodsNum(userId, goodsId, goodsNum);

            return Result.success(updateGoodsNum);
        }else{
            return Result.error("库存不足");
        }

    }

    @PostMapping("delete")
    @ApiOperation(value = "删除购物车", notes = "删除用购物车中的指定商品")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "userId", value = "用户ID"),
            @ApiImplicitParam(dataType = "List<Integer>", name = "goodsIds", value = "商品ID数组"),
    })
    public Result deleteGoods(@RequestBody DeleteGoodsDTO deleteGoodsDTO){

        List<Integer> goodsIds = deleteGoodsDTO.getGoodsIds();
        Integer userId = deleteGoodsDTO.getUserId();

        if (redisUtils.exists("Cart:"+userId)) {
            redisUtils.del("Cart:"+userId);
        }
        for (int goodsId : goodsIds) {
            cartService.deleteGoods(userId,goodsId);
        }

        return Result.success();
    }


}
